import { ref } from "vue";

export function uploadUtils() {
  // 图片路径
  const imageUrl = ref("");
  // 是否显示大图
  const dialogVisible = ref(false);

  // 选择文件后
  const handleChange = (file, fileList) => {
    imageUrl.value = file.url;
    var aa = document.querySelector(".el-upload--picture-card");
    if (fileList.length > 0) {
      aa.style.display = "none";
    }
  };

  // 删除文件时
  const beforeAvatarRemove = (file, fileList) => {
    var aa = document.querySelector(".el-upload--picture-card");
    if (fileList.length === 1) {
      aa.style.display = "inline-block";
    }
  };

  // 文件上传前
  const beforeAvatarUpload = (rawFile) => {
    if (rawFile.size / 1024 / 1024 > 2) {
      ElMessage.error("文件大小不能超过2MB!");
      return false;
    }
    return true;
  };

  // 图片放大
  const handlePictureCardPreview = (uploadFile) => {
    dialogVisible.value = true;
  };

  return {
    imageUrl,
    dialogVisible,
    handleChange,
    beforeAvatarUpload,
    beforeAvatarRemove,
    handlePictureCardPreview
  };
}

